using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Threading;
using Lucene.Linq.Storage;
using Lucene.Net.Store;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;

namespace Lucene.Linq.Azure.Test.WorkerRole
{
    public class WorkerRole : RoleEntryPoint
    {
        public override void Run()
        {
            // This is a sample worker implementation. Replace with your logic.
            Trace.WriteLine("$projectname$ entry point called", "Information");

            CloudStorageAccount account = AccountHelper.Account("Storage", true);
            Directory directory = new AzureDirectory(account, "lucene");
            StorageManager manager = new StorageManager(directory);

            while (true)
            {
                DemoPerson(manager, account);
                DemoJob(manager, account);
                Trace.WriteLine("Working", "Information");
            }
        }

        public override bool OnStart()
        {
            // Set the maximum number of concurrent connections 
            ServicePointManager.DefaultConnectionLimit = 12;

            // For information on handling configuration changes
            // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

            return base.OnStart();
        }

        private static void DemoPerson(StorageManager manager, CloudStorageAccount account)
        {
            Console.WriteLine("Add index type");
            manager.Add<Person>(new AzureBlobEntityStorage<Person>(account, "person"));

            manager.Add(new Person("1", "Bill"));

            manager.Add(new Person("2", "Sam"));

            // We should get only 2 records here
            foreach (Person person in manager.Search<Person>().Where(p => p.Id == "1"))
            {
                Console.WriteLine("{0} {1}", person.Id, person.Name);
            }

            foreach (string s in manager.GetAll(typeof(Person)))
            {
                Console.WriteLine("{0}", s);
            }

            manager.Remove(typeof(Person), "person1");

            manager.Remove(typeof(Person), "person2");
        }

        private static void DemoJob(StorageManager manager, CloudStorageAccount account)
        {
            manager.Add<Job>(new AzureTableEntityStorage<Job>(account, "job"));

            manager.Add(new Job("1", "Sell"));
            manager.Add(new Job("2", "Buy"));

            foreach (Job job in manager.Search<Job>().Where(p => p.Id == "1"))
            {
                Console.WriteLine("{0} {1}", job.Id, job.Name);
            }

            foreach (string s in manager.GetAll(typeof(Job)))
            {
                Console.WriteLine("{0}", s);
            }

            manager.Remove(typeof(Job), "job1");
            manager.Remove(typeof(Job), "job2");
            Console.ReadKey();
        }
    }
}
